layui.use(['table', 'admin', 'ax','element','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var  element=layui.element;
    var laydate=layui.laydate;
    //tab切换
    element.on('tab(doTabBrief)',function(data){
        console.log(data.index)
    });

    laydate.render({
        elem:'#startTime'
        ,value:new Date()
    });

    laydate.render({
        elem:'#endTime'
        ,value:new Date()
    });
    /**
     * 错漏拣管理
     */
    var HxErrorPick = {
        tableId: "hxErrorPickTable"
    };

    var HxErrorPick1 = {
        tableId: "storageStockManagementTable"
    };

    /**
     * 初始化表格的列
     */
    HxErrorPick.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'storageNumber', sort: true, title: '单据号'},
            {field: 'positionNumber', sort: true, title: '应拣库位'},
            {field: 'goodsNumber', sort: true, title: '应拣商品编号'},
            {field: 'goodsName', sort: true, title: '应拣商品'},
            {field: 'realNum', sort: true, title: '应拣数量  '},
            {field: 'realPositionNumber', sort: true, title: '实拣库位'},
            {field: 'realGoodsNumber', sort: true, title: '实拣商品编号'},
            {field: 'realGoodsName', sort: true, title: '实拣商品'},
            {field: 'rNum', sort: true, title: '实拣数量'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'stockPort', sort: true, title: '备货口'},
            {field: 'getUnit', sort: true, title: '收货客户'},
            {field: 'storageUserName', sort: true, title: '拣货人'},
            {field: 'errorPick', sort: true, title: '类型'},
            {field: 'status', sort: true, title: '处理状态'},

            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    HxErrorPick.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['storageNumber'] = $("#storageNumber").val();
        queryData['storageUserName'] = $("#storageUserName").val();
        queryData['goodsName'] = $("#goodsName").val();

        table.reload(HxErrorPick.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
                 // 搜索按钮点击事件
                 $('#btnSearch').click(function () {
                     HxErrorPick.search();
                 });
                 // 添加按钮点击事件
                 $('#btnAdd').click(function () {
                     HxErrorPick.openAddDlg();
                 });
                 // 导出excel
                 $('#btnExp').click(function () {
                     HxErrorPick.exportExcel();
                 });
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    HxErrorPick.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/hxErrorPick/add';
    };

    /**
     * 导出excel按钮
     */
    HxErrorPick.exportExcel = function () {
        var checkRows = table.checkStatus(HxErrorPick.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    HxErrorPick.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/hxErrorPick/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    HxErrorPick.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/hxErrorPick/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(HxErrorPick.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };
    HxErrorPick.onchuliItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/hxErrorPick/update", function (data) {
                Feng.success("处理成功!");
                table.reload(HxErrorPick.tableId);
            }, function (data) {
                Feng.error("处理失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否处理?", operation);
    };
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + HxErrorPick.tableId,
        url: Feng.ctxPath + '/hxErrorPick/list',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: HxErrorPick.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        HxErrorPick.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        HxErrorPick.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        HxErrorPick.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + HxErrorPick.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            HxErrorPick.openEditDlg(data);
        } else if (layEvent === 'delete') {
            HxErrorPick.onDeleteItem(data);
        }else if (layEvent === 'chuli'){
            HxErrorPick.onchuliItem(data);
        }
    });



    /**
     * 初始化表格的列
     */
        HxErrorPick1.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: '', title: '序号',type:"numbers"},
            {field: 'storageUserName', sort: true, title: '拣货人'},
            {field: 'numberErrors', sort: true, title: '合计'},
            {field: 'mispicking', sort: true, title: '错拣'},
            {field: 'underpicking', sort: true, title: '少拣'},
            {field: 'manaypicking', sort: true, title: '多拣'},
        ]];
    };

    // 渲染表格
    var tableResult1 = table.render({
        elem: '#' + HxErrorPick1.tableId,
        url: Feng.ctxPath + '/hxErrorPick/DetailsList',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: HxErrorPick1.initColumn()
    });


    /**
     * 点击查询按钮
     */
    HxErrorPick1.search = function () {
        var queryData = {};
        queryData['storageUserName'] = $("#storageUserName1").val();
        table.reload(HxErrorPick1.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
                // 搜索按钮点击事件
                $('#btnSearch2').click(function () {
                    HxErrorPick1.search();
                });
                // 导出excel
                $('#btnExp2').click(function () {
                    HxErrorPick1.exportExcel();
                });
            }
        });
    };
    // 搜索按钮点击事件
    $('#btnSearch2').click(function () {
        HxErrorPick1.search();
        table.reload('storageStockManagementTable');

    });

    // 导出excel
    $('#btnExp2').click(function () {
        HxErrorPick1.exportExcel();
    });
    /**
     * 导出excel按钮
     */
    HxErrorPick1.exportExcel = function () {
        var checkRows = table.checkStatus(HxErrorPick1.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult1.config.id, checkRows.data, 'xls');
        }
    };


});
